<template>
  <uni-nav-bar title="合作伙伴账号" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package">
    <view class="space-between">
      <view>昵称</view>
      <input class="uni-input" maxlength="10" placeholder="请输入昵称" v-model="nickname" />
    </view>
    <view class="space-between top">
      <view>手机号</view>
      <input class="uni-input" maxlength="11" placeholder="请输入手机号码" v-model="mobile" />
    </view>
    <view class="space-between top">
      <view>大客户专营ID</view>
      <hpy-form-select :dataList="CustMonList" text="name" name="value" @change="consolu($event, 'monopoly')"
        :title="monopolyName" :Text="monopolyName" :icon='true' />
    </view>
    <view class="space-between top">
      <view>大客户ID</view>
      <hpy-form-select :dataList="CustMonList" text="name" name="value" @change="consolu($event, 'CustMon')"
        :title="CustMonName" :Text="CustMonName" :icon='true' />
    </view>
    <view class="space-between top">
      <view>店铺专营ID</view>
      <hpy-form-select :dataList="StoreList" text="name" name="value" @change="consolu($event,'Store')"
        :title="StoreName" :Text="StoreName" :icon='true' />
    </view>
    <view class="space-between top">
      <view>商铺ID</view>
      <hpy-form-select :dataList="ShopList" text="name" name="value" @change="consolu($event,'Shop')" :title="ShopName"
        :Text="ShopName" :icon='true' />
    </view>
  </view>
  <view class="add" @click="BulletBox()">确认</view>
</template>

<script setup lang="ts">
  import { navBack } from '@/utils/navigator';
  import { ref } from 'vue';
  import { onLoad } from '@dcloudio/uni-app';
  import { toPublish } from '@mqtt';
  import { getAdminInfo, getCustMonList, getStoreList, getShopList } from '@/gql/administrator';
  import { errorToast, showLoading, successToast } from '@/utils/prompt';
  /**
   * 合作伙伴账号
   * @property {Number} accountID 合作伙伴账号ID
   * @property {String} nickname 昵称
   * @property {String} mobile 手机号
   * @property {String} ShopName 商铺名称
   * @property {Number} shop_id 商铺ID
   * @property {Array} ShopList 商铺列表
   * @property {String} StoreName 店铺专营名称
   * @property {Array} StoreList 店铺专营列表
   * @property {Number} store_id 店铺专营ID
   * @property {String} monopolyName 大客户专营名称
   * @property {String} CustMonName 大客户名称
   * @property {Number} customers_id 大客户ID
   * @property {Number} monopoly_id 大客户专营ID
   * @property {Array} CustMonList 大客户列表
   */
  const accountID = ref()
  const nickname = ref('')
  const mobile = ref('')

  const ShopName = ref('')
  const shop_id = ref()
  const ShopList = ref([])
  const StoreName = ref('')
  const store_id = ref()
  const StoreList = ref([])
  const monopolyName = ref('')
  const CustMonName = ref('')
  const customers_id = ref()
  const monopoly_id = ref()
  const CustMonList = ref([])
  onLoad((pearm) => {
    accountID.value = pearm.id
    init()
    getShop()
    getStore()
    getCustMon()
  })
  /**
   * 获取合作伙伴账号列表
   */
  function init() {
    showLoading()
    const payload = {
      query: getAdminInfo,
      variables: {
        admin_id: Number(accountID.value),
      },
    };
    toPublish(
      'ql/control/getAdminInfo',
      payload,
      (obj : any) => {
        const { getAdminInfo } = obj.data;
        nickname.value = getAdminInfo.nickname
        mobile.value = getAdminInfo.mobile
        ShopName.value = getAdminInfo.shop_name
        shop_id.value = getAdminInfo.shop_id
        StoreName.value = getAdminInfo.store_name
        store_id.value = getAdminInfo.store_id
        CustMonName.value = getAdminInfo.customers_name
        customers_id.value = getAdminInfo.customers_id
        monopolyName.value = getAdminInfo.monopoly_name
        monopoly_id.value = getAdminInfo.monopoly_id
      }
    );
  }
  // 商铺列表
  function getShop() {
    showLoading()
    const payload = {
      query: getShopList
    };
    toPublish(
      'ql/control/getShopList',
      payload,
      (obj : any) => {
        uni.hideLoading();
        const { getShopList } = obj.data;
        ShopList.value = getShopList
      }
    );
  }
  // 店铺列表
  function getStore() {
    showLoading()
    const payload = {
      query: getStoreList
    };
    toPublish(
      'ql/control/getStoreList',
      payload,
      (obj : any) => {
        const { getStoreList } = obj.data;
        StoreList.value = getStoreList
      }
    );
  }
  // 大客户列表
  function getCustMon() {
    showLoading()
    const payload = {
      query: getCustMonList
    };
    toPublish(
      'ql/control/getCustMonList',
      payload,
      (obj : any) => {
        uni.hideLoading();
        const { getCustMonList } = obj.data;
        CustMonList.value = getCustMonList
      }
    );
  }
  function consolu(e : any, type : string) {
    console.log(e.data, type)
    switch (type) {
      case 'monopoly':
        monopolyName.value = e.data.name
        monopoly_id.value = e.data.id
        break;
      case 'CustMon':
        CustMonName.value = e.data.name
        customers_id.value = e.data.id
        break;
      case 'Store':
        StoreName.value = e.data.name
        store_id.value = e.data.id
        break;
      case 'Shop':
        ShopName.value = e.data.name
        shop_id.value = e.data.id
        break;
    }
  }
  /**
   * 提交编辑合作伙伴账号
   * @property {Number} accountID 合作伙伴账号ID
   * @property {String} nickname 昵称
   * @property {String} mobile 手机号
   * @property {Number} shop_id 商铺ID
   * @property {Number} store_id 店铺专营ID
   * @property {Number} customers_id 大客户ID
   * @property {Number} monopoly_id 大客户专营ID
   */
  function BulletBox() {
    showLoading('正在提交');
    const payload = {
      admin_id: Number(accountID.value),
      customers_id: Number(customers_id.value),
      monopoly_id: Number(monopoly_id.value),
      store_id: Number(store_id.value),
      shop_id: Number(shop_id.value),
      nickname: nickname.value,
      mobile: mobile.value
    };
    toPublish('control/editAdminInfo', payload, (obj : any) => {
      const { code, msg } = obj;
      if (code === 1) {
        successToast('提交成功');
        init()
      } else {
        errorToast(msg);
      }
    });
  }
</script>

<style scoped lang="less">
  .add {
    width: 480rpx;
    height: 100rpx;
    background: linear-gradient(-10deg, #2C96F9, #1B91FF);
    border-radius: 5px;
    text-align: center;
    line-height: 100rpx;
    color: #fff;
    margin: 60rpx auto 20rpx;
    font-size: 32rpx;
  }

  .top {
    margin-top: 20rpx;
  }

  .package {
    input {
      text-align: right;
    }
  }
</style>
<style>
  .top>>>.select-picker {
    justify-content: flex-end;
  }
</style>